package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Advertisement;
import connection.ServiceProxy;

/**
 * Servlet implementation class AddToCart
 */
public class AddToCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddToCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String message = null, result = null, destination = null;
		boolean error = false;
		int orderedQuantity =0, itemQuantity, quantityInCart = 0;
		String itemId =  request.getParameter("itemId");
		String cartItemId = request.getParameter("cartItemId");
		if(null != itemId) {
			//int ite
			System.out.println("harr " + request.getParameter("orderedQuantity"));
			if(request.getParameter("orderedQuantity").equals("")) {
				error = true;
				message = "Please enter item quantity";
			}
			else if(request.getParameter("orderedQuantity").equals("0")) {
				error = true;
				message = "Please enter item quantity greater than 0";
			}
			
			if(error == false) {
				try {
					orderedQuantity = Integer.parseInt(request.getParameter("orderedQuantity"));
				}
				catch(NumberFormatException e) {
					error = true;
					message = "Please enter correct value of quantity";
				}
			}
			
			if(error == false) {
				itemQuantity = Integer.parseInt(request.getParameter("itemQuantity"));
			
				if(request.getParameter("submit").equals("Add to Cart")){
					quantityInCart = Integer.parseInt(request.getParameter("quantityInCart"));
					orderedQuantity += quantityInCart; 
				}
				
				if(orderedQuantity > itemQuantity) {
					error = true;
					message = "Order Quantity cannot be greater than Available Quantity";
				}
			}
			
			if(error == false) {
				HttpSession session = request.getSession();
				String userId = (String)session.getAttribute("userId");
				ServiceProxy oProxy = new ServiceProxy();
				if(cartItemId.equals("0")) {
					result = oProxy.addItemToCart(userId,itemId,(String)request.getParameter("orderedQuantity"));
				}
				else
					result = oProxy.updateItemInCart(cartItemId,"" + orderedQuantity);
				System.out.println("Result" + result);
				if(result.equals("true")) {
					message = "Item added Successfully";					
				}
				else {
					error = true;
					message = "Please try again";
				}
					
			}
		}
		
		if(error == true) {
			destination = "/ViewOtherAdvertisement";
		}
		else {
			destination = "/ViewCart";
		}
		
		request.setAttribute("message", message);
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
		rd.forward(request, response);
	}

}
